﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookOnline.Data
{
    public class GetData:SqlDataProvider
    {
        public DataSet ExcuteProc(string procName)
        {
            DataSet ds = new DataSet();
            SqlConnection connect = getConnect();
            SqlCommand com = new SqlCommand(procName,connect);
            com.CommandType = CommandType.StoredProcedure;
            if (connect.State == ConnectionState.Closed || connect.State == ConnectionState.Broken)
            {
                connect.Open();
            }
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(com);
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex);
            }
            finally
            {
                com.Dispose();
                connect.Close();
            }
        }

        public DataTable ExcuteProc(string procName, SqlParameter ID)
        {
            DataTable table = new DataTable();
            SqlConnection con = getConnect();
            SqlCommand com = new SqlCommand(procName, con);
            com.CommandType = CommandType.StoredProcedure;
            if (con.State == ConnectionState.Closed || con.State == ConnectionState.Broken)
            {
                con.Open();
            }
            try
            {
                if (ID != null)
                {
                    com.Parameters.Add(ID);
                }
                SqlDataAdapter da = new SqlDataAdapter(com);
                da.Fill(table);
                return table;
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex);
            }
            finally
            {
                com.Dispose();
                con.Close();
            }
        }

        public int ExcuteProc(string procName, params SqlParameter[] arrParam)
        {
            SqlConnection con = getConnect();
            SqlCommand com = new SqlCommand(procName, con);
            if (con.State == ConnectionState.Closed || con.State == ConnectionState.Broken)
            {
                con.Open();
            }
            try
            {
                if (arrParam != null)
                {
                    foreach (SqlParameter param in arrParam)
                    {
                        com.Parameters.Add(param);
                    }
                }
                int i = com.ExecuteNonQuery();
                return i;
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex);
            }
            finally
            {
                com.Dispose();
                con.Close();
            }
        }
    }
}
